package com.youku.mson.serializer;

import java.lang.reflect.Array;

public class ArraySerializer implements JsonSerializer {

	@Override
	public void serialize(JsonWriter out, Object target) {
		if (target == null) {
			out.writeNull();
		} else {
			out.beginArray();
			for (int i = 0; i < Array.getLength(target); i++) {
				Object element = Array.get(target, i);

				JsonSerializer serializer = JsonSerializerFactory.getSerializer(target.getClass().getComponentType());

				serializer.serialize(out, element);
				
				out.writeComma();
			}

			if (out.endWith(",")) {
				out.deleteEnd();
			}

			out.endArray();
		}

	}

}
